import { controller, httpGet as Get, httpPost as Post } from 'inversify-express-utils';
import { inject } from 'inversify';
import { Request, Response } from 'express'
import { UserService } from './service'
import { JWT } from '../jwt';
// 此文件类似于路由,分别流向不同的服务模块处理对应的逻辑
@controller('/user')
export class User {
  // 将service实例注如到当前类中, 固定写法 类似于springboot中的 @Autowire 或者 @Resource
  constructor(@inject(UserService) private readonly userService: UserService){}

  @Get('/index', JWT.jwtMiddleware())
  public async getIndex(req: Request, res: Response) {
    // request.user 是经过身份验证后的用户对象。当使用 Passport 等身份验证中间件对请求进行身份验证后，会将表示用户的对象存储在 request.user 中。
    console.log(req.user.id)
    const result = await this.userService.getList()
    res.send(result)
  }

  @Post('/add')
  public async add(req: Request, res: Response) {
    console.log(req.body)
    const result = await this.userService.addUser(req.body)
    res.send(result)
  }
}